#Andrew Gooch
#April 11, 2017
#Ripping Yarn, Political Communications
#Replication R file 3: Figure 2 Social Security support plot

#This R file creates Figure 2 using ggplot.

#REQUIREMENTS:
#Must run March_weights.R before running code below


#AVERAGE TREATMENT EFFECTS# REFERENCED IN TEXT
svymean(~SuppCont,design=Mar15.svy.rake, na.rm=TRUE)
confint(svymean(~SuppCont,design=Mar15.svy.rake, na.rm=TRUE))
svymean(~SuppPerStory,design=Mar15.svy.rake, na.rm=TRUE)
confint(svymean(~SuppPerStory,design=Mar15.svy.rake, na.rm=TRUE))


##########################################
# mean support and confidence intervals MARCH
##########################################

demsub <- subset(Mar15.svy.rake, PID3_lean_new==1)

demsControl_mean_Mar15 <- svymean(~SuppCont,design=demsub, na.rm=TRUE)[1]
demsControl_lower_Mar15 <- confint(svymean(~SuppCont,design=demsub, na.rm=TRUE))[1]
demsControl_upper_Mar15 <- confint(svymean(~SuppCont,design=demsub, na.rm=TRUE))[2]

demsPerStory_mean_Mar15 <- svymean(~SuppPerStory,design=demsub, na.rm=TRUE)[1]
demsPerStory_lower_Mar15 <- confint(svymean(~SuppPerStory,design=demsub, na.rm=TRUE))[1]
demsPerStory_upper_Mar15 <- confint(svymean(~SuppPerStory,design=demsub, na.rm=TRUE))[2]

indsub <- subset(Mar15.svy.rake, PID3_lean_new==0)

indsControl_mean_Mar15 <- svymean(~SuppCont,design=indsub, na.rm=TRUE)[1]
indsControl_lower_Mar15 <- confint(svymean(~SuppCont,design=indsub, na.rm=TRUE))[1]
indsControl_upper_Mar15 <- confint(svymean(~SuppCont,design=indsub, na.rm=TRUE))[2]

indsPerStory_mean_Mar15 <- svymean(~SuppPerStory,design=indsub, na.rm=TRUE)[1]
indsPerStory_lower_Mar15 <- confint(svymean(~SuppPerStory,design=indsub, na.rm=TRUE))[1]
indsPerStory_upper_Mar15 <- confint(svymean(~SuppPerStory,design=indsub, na.rm=TRUE))[2]

repsub <- subset(Mar15.svy.rake, PID3_lean_new==-1)

repsControl_mean_Mar15 <- svymean(~SuppCont,design=repsub, na.rm=TRUE)[1]
repsControl_lower_Mar15 <- confint(svymean(~SuppCont,design=repsub, na.rm=TRUE))[1]
repsControl_upper_Mar15 <- confint(svymean(~SuppCont,design=repsub, na.rm=TRUE))[2]

repsPerStory_mean_Mar15 <- svymean(~SuppPerStory,design=repsub, na.rm=TRUE)[1]
repsPerStory_lower_Mar15 <- confint(svymean(~SuppPerStory,design=repsub, na.rm=TRUE))[1]
repsPerStory_upper_Mar15 <- confint(svymean(~SuppPerStory,design=repsub, na.rm=TRUE))[2]


##########################################
# 3-panel plot, one for each PID, MARCH
##########################################

#first panel plot -

coef <- c(demsControl_mean_Mar15,
          demsPerStory_mean_Mar15)

lb <-c(demsControl_lower_Mar15,
       demsPerStory_lower_Mar15)

ub <-c(demsControl_upper_Mar15,
       demsPerStory_upper_Mar15)


method <-c("Control Group",
           "Personalized Story Group")


specification <-c(1,2)


df <- data.frame(coef, method, specification,lb,ub)

p1 <- ggplot(df, aes(specification,coef, color=method)) +
  geom_point(aes(shape=method),size=5, position = position_dodge(width = 0.2)) + 
  scale_color_manual(name=" ",values=c("blue","blue")) + 
  scale_shape_manual(name=" ", values=c(17,19)) + 
  theme_bw() +
  scale_x_continuous(" ", breaks=1, labels=c(" ")) + 
  labs(title = "Democrats") +
  scale_y_continuous("Support for Social Security Reform (0-1)", limits=c(0.45,.86)) +
  geom_errorbar(aes(ymin=lb,ymax=ub),width = 0, position = position_dodge(width = 0.2), size=2) + 
  #geom_text(aes(2, 28, label="Biden Cue"),size=5, color="black") +
  #geom_text(aes(1, -10, label="R"),size=6, color="black") +
  #geom_text(aes(2, -24, label="I"),size=6, color="black") +
  #geom_text(aes(3, -14.4, label="D"),size=6, color="black") +
  theme(axis.text.x=element_text(size=rel(1.3)),
        axis.title.x=element_text(size=rel(1.7)),
        axis.title.y=element_text(size=rel(1.3)),
        axis.text.y=element_text(size=rel(1.7)),
        panel.grid.minor=element_blank(),
        panel.grid.major.x=element_blank(),
        axis.ticks.y = element_blank(), 
        axis.ticks.x = element_blank(),
        panel.border = element_blank(),
        axis.line.x = element_line(color = 'black'),
        legend.key.size = unit(1., "cm"),
        legend.text=element_text(size=rel(1.7)), 
        legend.key = element_blank(),
        legend.title=element_blank(),
        plot.title = element_text(size = rel(1.7)),
        legend.direction = "horizontal", 
        #legend.justification=c(1,0), 
        legend.position=c(.50,.95)
  )

print(p1)

#second panel plot - 

coef <- c(indsControl_mean_Mar15,
          indsPerStory_mean_Mar15)

lb <-c(indsControl_lower_Mar15,
       indsPerStory_lower_Mar15)

ub <-c(indsControl_upper_Mar15,
       indsPerStory_upper_Mar15)


method <-c("Control Group",
           "Personalized Story Group")


specification <-c(1,2)


df <- data.frame(coef, method, specification,lb,ub)

p2 <- ggplot(df, aes(specification,coef, color=method)) +
  geom_point(aes(shape=method),size=5, position = position_dodge(width = 0.2)) + 
  scale_color_manual(name=" ",values=c("grey","grey")) + 
  scale_shape_manual(name=" ", values=c(17,19)) + 
  theme_bw() +
  scale_x_continuous(" ", breaks=1, labels=c(" ")) + 
  labs(title = "Independents") +
  scale_y_continuous(" ", limits=c(0.45,.86)) +
  geom_errorbar(aes(ymin=lb,ymax=ub),width = 0, position = position_dodge(width = 0.2), size=2) + 
  #geom_text(aes(2, 28, label="Biden Cue"),size=5, color="black") +
  #geom_text(aes(1, -10, label="R"),size=6, color="black") +
  #geom_text(aes(2, -24, label="I"),size=6, color="black") +
  #geom_text(aes(3, -14.4, label="D"),size=6, color="black") +
  theme(axis.text.x=element_text(size=rel(1.3)),
        axis.title.x=element_text(size=rel(1.7)),
        axis.title.y=element_text(size=rel(1.3)),
        axis.text.y=element_text(size=rel(1.7)),
        panel.grid.minor=element_blank(),
        panel.grid.major.x=element_blank(),
        axis.ticks.y = element_blank(), 
        axis.ticks.x = element_blank(),
        panel.border = element_blank(),
        axis.line.x = element_line(color = 'black'),
        legend.key.size = unit(1., "cm"),
        legend.text=element_text(size=rel(1.7)), 
        legend.key = element_blank(),
        legend.title=element_blank(),
        plot.title = element_text(size = rel(1.7)),
        legend.direction = "horizontal", 
        #legend.justification=c(1,0), 
        legend.position=c(.50,.95)
  )

print(p2)


#second panel plot - anonymous stories

coef <- c(repsControl_mean_Mar15,
          repsPerStory_mean_Mar15)

lb <-c(repsControl_lower_Mar15,
       repsPerStory_lower_Mar15)

ub <-c(repsControl_upper_Mar15,
       repsPerStory_upper_Mar15)


method <-c("Control Group",
           "Personalized Story Group")


specification <-c(1,2)


df <- data.frame(coef, method, specification,lb,ub)

p3 <- ggplot(df, aes(specification,coef, color=method)) +
  geom_point(aes(shape=method),size=5, position = position_dodge(width = 0.2)) + 
  scale_color_manual(name=" ",values=c("red","red")) + 
  scale_shape_manual(name=" ", values=c(17,19)) + 
  theme_bw() +
  scale_x_continuous(" ", breaks=1, labels=c(" ")) + 
  labs(title = "Republicans") +
  scale_y_continuous(" ", limits=c(.45,.86)) +
  geom_errorbar(aes(ymin=lb,ymax=ub),width = 0, position = position_dodge(width = 0.2), size=2) + 
  #geom_text(aes(2, 28, label="Biden Cue"),size=5, color="black") +
  #geom_text(aes(1, -10, label="R"),size=6, color="black") +
  #geom_text(aes(2, -24, label="I"),size=6, color="black") +
  #geom_text(aes(3, -14.4, label="D"),size=6, color="black") +
  theme(axis.text.x=element_text(size=rel(1.3)),
        axis.title.x=element_text(size=rel(1.7)),
        axis.title.y=element_text(size=rel(1.3)),
        axis.text.y=element_text(size=rel(1.7)),
        panel.grid.minor=element_blank(),
        panel.grid.major.x=element_blank(),
        axis.ticks.y = element_blank(), 
        axis.ticks.x = element_blank(),
        panel.border = element_blank(),
        axis.line.x = element_line(color = 'black'),
        legend.key.size = unit(1., "cm"),
        legend.text=element_text(size=rel(1.7)), 
        legend.key = element_blank(),
        legend.title=element_blank(),
        plot.title = element_text(size = rel(1.7)),
        legend.direction = "horizontal", 
        #legend.justification=c(1,0), 
        legend.position=c(.50,.95)
  )

print(p3)

g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

mylegend<-g_legend(p2)

p4 <- grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
                               p2 + theme(legend.position="none"),
                               p3 + theme(legend.position="none"),
                               nrow=1),
                   mylegend, nrow=2,heights=c(10, 1))




